package drds.global_transaction;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import drds.common.Author;
import drds.dbutils.native_sql.Db;
import drds.global_transaction.image.UndoLog;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.atomic.AtomicBoolean;

@Slf4j
@Author(name = Authors.LI_YANG)
public class TransactionTimeOutDealWith
{
    private int timeoutSeconds = 30 * 1000;
    private int expireMilliSeconds = 15 * 1000;

    public void timingExecution()
    {
        long current = System.currentTimeMillis();

        //if (transaction.isRunnable())
        {
            BranchTransactionInfo branchTransactionInfo = null;
            if (branchTransactionInfo.isTimeOut())
            {
                //expiredTransactions.add(transaction);
            }
        } // end-if (transaction.isRunnable())


            /*if (transaction.getTransactionStatus() == Status.STATUS_ACTIVE
                    || transaction.getTransactionStatus() == Status.STATUS_MARKED_ROLLBACK)
            {
                this.timingRollback(transaction);
            }*/

        //清除超期lock
        rollbackOverTimeImage();
    }

    private void rollbackOverTimeImage()
    {
        long currentTimeMillis = System.currentTimeMillis();
        Connection connection = null;
        try
        {
            String sql = "select gtul.id, gtul.undo_log,gtl.create_date_time_long from global_transaction_lock gtl,global_transaction_undo_log gtul where gtl.global_transaction_id=gtul.global_transaction_id and gtl.branch_transaction_id=gtul.branch_transaction_id and  gtl.create_date_time_long +? < ? ";
            AtomicBoolean isExpire = new AtomicBoolean(false);
            ResultSet resultSet = Db.query(connection, sql, new Object[]{expireMilliSeconds, currentTimeMillis});
            while (resultSet.next())
            {
                Long create_date_time_long = resultSet.getLong("create_date_time_long");
                if (currentTimeMillis - create_date_time_long < timeoutSeconds)
                {
                    log.debug("Not go to expired txc,continue!");
                    //return null;
                }

                isExpire.set(true);
                String imageInfo = resultSet.getString("rollback_info");
                UndoLog undoLog = JSON.parseObject(imageInfo, new TypeReference<UndoLog>()
                {

                });
                undoLog.setId(resultSet.getLong("id"));
                undoLog.rollback();
                undoLog.updateStatusFinish(connection);
                //return null;
            }
            resultSet.close();

            if (isExpire.get())
            {
                sql = "select count(*) as total from global_transaction_lock where  create_date_time_long +? < ?";
                 resultSet = Db.query(connection, sql, new Object[]{expireMilliSeconds, currentTimeMillis});
                resultSet.next();
                int total = resultSet.getInt("total");
                resultSet.close();
                if (total > 0)
                {
                    sql = "delete from global_transaction_lock where  create_date_time_long +?<? ";
                    Db.update(connection, sql, new Object[]{expireMilliSeconds, currentTimeMillis});
                }
            }
        } catch (SQLException e)
        {
            log.error("SQLException", e);
        }

    }

}
